package com.gopay.bis.service.bank.impl.wap;

import com.gopay.bis.manager.bank.NuccSerialManager;
import com.gopay.bis.manager.bank.Wap.NuccWapB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.NuccConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.nucc.ebank.pay.res.NuccEbankRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayInteractRemoteService;
import javax.annotation.Resource;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

/**
 * 网联网关支付WAPB2C
 * @NuccWapPayB2cBankServiceImpl.java
 * @author zhoutong
 * @2018-1-17 下午1:51:26 www.gopay.com.cn Inc.All rights reserved.
 */
public class NuccWapPayB2cBankServiceImpl implements WapPayInteractRemoteService {

	private static Logger logger = LoggerFactory.getLogger(NuccWapPayB2cBankServiceImpl.class);

	@Resource(name = "nuccWapB2cManager")
	private NuccWapB2cManager nuccWapB2cManager;

	@Resource(name = "nuccSerialManager")
	private NuccSerialManager nuccSerialManager;

	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			nuccSerialManager.generateFlowIdBySerial(base, true, NuccConstants.NUCC_SERIAL_CONTROL_GW);
			NuccEbankRes res = (NuccEbankRes) nuccWapB2cManager.sign(base);
			if (!NuccConstants.NUCC_SYSRTNCD_SUCCESS.equals(res.getMsgBody().getSysRtnInf().getSysRtnCd())) {
				logger.error("网联网关支付申请异常，系统返回码为"+res.getMsgBody().getSysRtnInf().getSysRtnCd()+":"+res.getMsgBody().getSysRtnInf().getSysRtnDesc());
				nuccSerialManager.getFlowIdRule();
				throw new GopayException(res.getMsgBody().getSysRtnInf().getSysRtnCd(),res.getMsgBody().getSysRtnInf().getSysRtnDesc());
			}
			if (!NuccConstants.NUCC_BIZSTSCD_SUCCESS.equals(res.getMsgBody().getBizInf().getBizStsCd())) {
				logger.error("网联网关支付申请异常，业务返回码为"+res.getMsgBody().getBizInf().getBizStsCd()+":"+res.getMsgBody().getBizInf().getBizStsDesc());
				throw new GopayException(res.getMsgBody().getBizInf().getBizStsCd(),res.getMsgBody().getBizInf().getBizStsDesc());
			}
			if (!NuccConstants.NUCC_TRXSTATUSCD_PROCESSING.equals(res.getMsgBody().getBizInf().getTrxStatus())) {
				logger.error("网联网关支付申请异常，交易状态为"+res.getMsgBody().getBizInf().getTrxStatus());
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E6128);
			}
			res.setSignMessage(Base64.encodeBase64String(StringUtils.getBytesUtf8(res.getPlainMessage())));
			base.setSignMessage(res.getSignMessage());
			BeanUtils.copyProperties(base, res);
			result.setDefaultModel(res);
			result.setSuccess(true);
		} catch (GopayException e) {
			logger.error("nucc wap b2c exception：", e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			logger.error("nucc wap b2c exception：", ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6128);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		return null;
	}

	@Override
	public Result query(BaseMessage base) {
		return null;
	}

	@Override
	public Result refund(BaseMessage base) {
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		return null;
	}
}
